package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomExceotion;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
   private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void delete(Long id) {
        //查看菜品下有没有关联菜品
        LambdaQueryWrapper<Dish> lam = new LambdaQueryWrapper<>();
        lam.eq(Dish::getCategoryId,id);
        int count = dishService.count(lam);
        if (count>0){
            throw new CustomExceotion("该菜品有关联菜品,不能删除");
        }
        //查看套餐下有没有关联菜品
        LambdaQueryWrapper<Setmeal> lam2 = new LambdaQueryWrapper<>();
        lam2.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(lam2);
        if (count2>0){
            throw new CustomExceotion("该菜品有关联套餐,不能删除");
        }
        baseMapper.deleteById(id);

    }
}
